package com.mywang.weitravel.modules.find;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mywang.weitravel.R;
import com.mywang.weitravel.adapter.FindListAdapter;
import com.mywang.weitravel.base.BaseFragment;
import com.mywang.weitravel.model.FindListBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import butterknife.BindView;

/**
 * Created by wangmingyun on 2019/12/5.
 */
public class FindFragment extends BaseFragment<FindPresenter> implements FindContract.View {

    @BindView(R.id.rv_find_list)
    RecyclerView rvFindList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    private FindListAdapter findListAdapter;
    private int page = 1;

    @Override
    protected FindPresenter initPresenter() {
        return new FindPresenter(this);
    }

    @Override
    protected int getFragmentLayoutID() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        ivTitleBack.setVisibility(View.GONE);
        tvTitle.setText("发现");
        findListAdapter = new FindListAdapter(mActivity);
        findListAdapter.setOnItemOnclickListener(new FindListAdapter.OnItemOnclickListener() {
            @Override
            public void onItemClick(FindListBean.DataBean data, int position) {
                FindDetailActivity.launch(getActivity(), data.getId());
            }

            @Override
            public void collect(FindListBean.DataBean data) {
                if (goLoginActivity(getActivity())) {
                    mPresenter.collect(data.getId());
                }
            }
        });
        //设置layoutManager
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        //解决item跳动
        manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        rvFindList.setLayoutManager(manager);
        rvFindList.setAdapter(findListAdapter);
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                mPresenter.getFindList(page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                mPresenter.getFindList(page);
            }
        });
        refreshLayout.autoRefresh();
    }

    @Override
    public void getFindListSuccess(FindListBean findListBean) {
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
        if (findListBean.getList().size() > 0) {
            if (page == 1) {
                findListAdapter.refreshData(findListBean.getList());
            } else {
                findListAdapter.addData(findListBean.getList());
            }
        } else {
            showFailed("暂无数据");
        }
    }

    @Override
    public void getFindListError() {
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void collectSuccessful(String findId) {
        findListAdapter.collect(findId);
    }
}
